<?php
/*
* $Author ：PHPYUN开发团队
*
* 官网: http://www.phpyun.com
*
* 版权所有 2009-2019 宿迁鑫潮信息技术有限公司，并保留所有权利。
*
* 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */
class compete_model extends model{	

	/**
	 * @desc  企业发布职位竞争力统计
	 */
	public function comJob($uid,$jobid){
		
		$compete	=	array();
		$jobInfo = $this -> select_once('company_job',array('id' => (int)$jobid,'uid' => (int)$uid),'`id`,`uid`,`minsalary`,`maxsalary`,`edu`,`exp`,`job1_son`,`name`,`welfare`');
		
		if($jobInfo['job1_son'])
		{
			$sameJobWhere['job1_son']	= $jobInfo['job1_son'];
			$sameJobWhere['uid']		= array('<>',$jobInfo['uid']);
		}elseif($jobInfo['name']){

			$sameJobWhere['name']	= array('like',$jobInfo['name']);
			$sameJobWhere['uid']	= array('<>',$jobInfo['uid']);
		}
		if($sameJobWhere){

			$sameJobWhere['orderby']  =	array('lastupdate,desc');
			$sameJobWhere['limit']	  =	'2000';

			$sameJobList = $this -> select_all('company_job',$sameJobWhere,'`uid`,`minsalary`,`welfare`,`exp`,`edu`');
		}
		
		if(!empty($sameJobList))
		{	
			include(PLUS_PATH.'com.cache.php');

			$jobNum = count($sameJobList);

			$competeWelfare	=	array();
			
			foreach($sameJobList as $key => $value)
			{
				

				$salaryCpt		= $this -> salaryCompete($value['minsalary']);
				$competeKey		= $salaryCpt['competeKey'];
				$competeName	= $salaryCpt['competeName'];

				
				$compete		=	$this -> competeStatic($compete,'salary',$competeKey,$competeName,$jobNum);

				if($value['edu']){
					$compete	=	$this -> competeStatic($compete,'edu',array_search($value['edu'],$comdata['job_edu']),$comclass_name[$value['edu']],$jobNum);
				}

				if($value['exp']){
					$compete	=	$this -> competeStatic($compete,'exp',array_search($value['edu'],$comdata['job_edu']),$comclass_name[$value['exp']],$jobNum);
				}

				if($value['welfare']){
				
					$welfare		=	@explode(',',$value['welfare']);
					$competeWelfare =	array_unique(array_merge($welfare,$competeWelfare));

				}
			}

			$salaryCpt		= $this -> salaryCompete($jobInfo['minsalary']);
			
			$jobKey		= $salaryCpt['competeKey'];

			$compete['salaryStatis']	=	$this -> perceStatis($compete['salary'],$jobKey);

			if($jobInfo['edu']){
				$compete['eduStatis']		=	$this -> perceStatis($compete['edu'],array_search($jobInfo['edu'],$comdata['job_edu']));
				
			}else{
				$compete['eduStatis'] = array();
			}

			if($jobInfo['exp']){
				$compete['expStatis']		=	$this -> perceStatis($compete['exp'],array_search($jobInfo['exp'],$comdata['job_exp']));
			}else{
				$compete['expStatis'] = array();
			}
			
			if($jobInfo['welfare']){
				
				$welfare	=	explode(',',$jobInfo['welfare']);
				foreach($welfare as $key => $value){
					
					if(!in_array($value,$competeWelfare)){

						$compete['welfare']['my'][] = $value;

					}else{
						$welKey	=	array_search($value,$competeWelfare);
						unset($competeWelfare[$welKey]);
					}
				}
			}
			$compete['welfare']['else'] = $competeWelfare;
			 
			$score = 0;
			if($compete['salaryStatis']['type'] == 1){
				$score		+=	40;
				$scoreMsg[]	=	'最低薪资高于行业平均水平';
			}else{
				$scoreMsg[]	=	'最低薪资低于行业平均水平';
			}

			if($compete['expStatis']['type'] == 2){
				$score		+=	25;
				$scoreMsg[]	=	'工作经验要求低于同行业';
			}else{
				$scoreMsg[]	=	'工作经验要求高于同行业';
			}
			if($compete['eduStatis']['type'] == 2){
				$score		+=	25;
				$scoreMsg[]	=	'学历要求低于同行业';
			}else{
				$scoreMsg[]	=	'学历要求高于同行业';
			}
			if(!empty($compete['welfare']['my'])){
				$score += 10;
				$scoreMsg[]	=	'拥有相应福利待遇优势';
			}else{
				$scoreMsg[]	=	'相应福利待遇没有优势';
			}
			if($score<40){
				$compete['scoreName']	=	'低';
			}elseif($score>=40 && $score<=60){
				$compete['scoreName']	=	'中';
			}elseif($score>60){
				$compete['scoreName']	=	'高';
			}
			$compete['scoreMsg']	=	implode(',',$scoreMsg);
			$compete['score']		=	$score;

			$compete['jobinfo']['job_salary']	=	$this -> salaryShow($jobInfo['minsalary'] , $jobInfo['maxsalary']);
			
			$compete['jobinfo']['job_edu']		=	$comclass_name[$jobInfo['edu']];

			$compete['jobinfo']['job_exp']		=	$comclass_name[$jobInfo['exp']];

			$compete['jobinfo']['name']			=	$jobInfo['name'];

			$compete['errcode'] = '1';
		}else{
			$compete['errcode'] = '2';

		
		}
		
		return $compete;
	}


	function userJob($uid,$jobid,$usertype){
		
		
		if(!$uid){
			$compete['errcode'] = '3'; 
			return $compete;
		}

		if($usertype != 1){
			$compete['errcode'] = '5'; 
			return $compete;
		}

		$expectInfo =	$this -> select_once('resume_expect',array('uid' => (int)$uid , 'defaults'=>1),'`minsalary`,`maxsalary`,`edu`,`exp`');
		if(empty($expectInfo)){
			$compete['errcode'] = '4'; 
			return $compete;
		}
		$lookLog	=	$this -> select_all('look_job',array('jobid' => (int)$jobid ,'orderby' => 'datetime,desc','limit' =>'1000'),'`uid`');
		if(empty($lookLog)){
			$compete['errcode'] = '2'; 
			return $compete;
		}
		if(!empty($lookLog)){
			
			foreach($lookLog as $key => $value){
				
				if($value['uid'] != $uid){
					$lookUid[$value['uid']]	=	$value['uid'];
				}
				
			}
			
			$expectList =	$this -> select_all('resume_expect',array('uid' => array('in',pylode(',',$lookUid)),'defaults'=>1),'`uid`,`minsalary`,`edu`,`exp`');
			
			
			if(empty($expectList)){
			
				$compete['errcode'] = '2';
				return $compete;
				
			}else{	
				include(PLUS_PATH.'user.cache.php');

				$expectNum		=	count($expectList);

				$competeWelfare	=	array();

				foreach($expectList as $key => $value)
				{
					
					$salaryCpt		= $this -> salaryCompete($value['minsalary']);
					$competeKey		= $salaryCpt['competeKey'];
					$competeName	= $salaryCpt['competeName'];
					
					$compete		=	$this -> competeStatic($compete,'salary',$competeKey,$competeName,$expectNum);

					if($value['edu']){
						$compete	=	$this -> competeStatic($compete,'edu',array_search($value['edu'],$userdata['user_edu']),$userclass_name[$value['edu']],$expectNum);
					}
					
					if($value['exp']){
						$compete	=	$this -> competeStatic($compete,'exp',array_search($value['exp'],$userdata['user_word']),$userclass_name[$value['exp']],$expectNum);
					}
					
				}

				$salaryCpt		= $this -> salaryCompete($expectInfo['minsalary']);
				$userKey		= $salaryCpt['competeKey'];
				
				$compete['salaryStatis']	=	$this -> perceStatis($compete['salary'],$userKey);

				if($expectInfo['edu']){
					$compete['eduStatis']	=	$this -> perceStatis($compete['edu'],array_search($expectInfo['edu'],$userdata['user_edu']));
					
				}
				
				
				if($expectInfo['exp']){
					$compete['expStatis']	=	$this -> perceStatis($compete['exp'],array_search($expectInfo['exp'],$userdata['user_word']));
				}

			}
			 
			$score = 0;
			if($compete['salaryStatis']['type'] == 2){
				
				$scoreMsg[]	=	'期望薪资高于平均水平';
			}else{
				$score		+=	45;
				$scoreMsg[]	=	'期望薪资低于平均水平';
			}

			if($compete['expStatis']['type'] == 2){
				
				$scoreMsg[]	=	'工作经验低于同行';
			}else{
				$score		+=	30;
				$scoreMsg[]	=	'工作经验高于同行';
			}
			if($compete['eduStatis']['type'] == 2){
				
				$scoreMsg[]	=	'学历低于同行';
			}else{
				$score		+=	35;
				$scoreMsg[]	=	'学历高于同行';
			}
			
			if($score<45){
				$compete['scoreName']	=	'低';
			}elseif($score>=45 && $score<=65){
				$compete['scoreName']	=	'中';
			}elseif($score>65){
				$compete['scoreName']	=	'高';
			}
			$compete['scoreMsg']	=	implode(',',$scoreMsg);
			$compete['score']		=	$score;

			
			$compete['resinfo']['user_salary']	=	$this -> salaryShow($expectInfo['minsalary'] , $expectInfo['maxsalary']);
			$compete['resinfo']['user_edu']		=	$userclass_name[$expectInfo['edu']];

			$compete['resinfo']['user_exp']		=	$userclass_name[$expectInfo['exp']];
			$compete['errcode'] = '1';
			
		}

		return $compete;
	
	
	}

	private function salaryShow($minsalary,$maxsalary){
		
 		if ($minsalary || $maxsalary) {
				 
			if($minsalary && $maxsalary){
				
				$salary	=  '￥'.$minsalary.'-'.$maxsalary;
				
			}elseif ($minsalary){
				
				$salary	=  '￥'.$minsalary;
				
			}else{
				
				$salary	=  '面议';
				
			}
		
		}else{
			
			$salary		=  '面议';
			
		}
	
		return $salary;
	
	}
	
	/**
	 * @desc 薪资区间定位
	 */
	Private function salaryCompete($salary){
		
		if($salary>0)
		{
			if($salary>=10000)
			{
				$competeKey  = 10001;
				$competeName = '1万以上';
			}elseif($salary>=8000){
				$competeKey  = 8001;
				$competeName = '8K-10K';
			}elseif($salary>=5000){
				$competeKey  = 5001;
				$competeName = '5K-8K';
			}elseif($salary>=3000){
				$competeKey  = 3001;
				$competeName = '3K-5K';
			}else{
				$competeKey  = 3000;
				$competeName = '3000以下';
			}
		
		}else{
			
			$competeKey  = 0;
			$competeName = '面议';
		
		}

		return array('competeKey' => $competeKey , 'competeName' => $competeName);
	
	}
	
	/**
	 * @desc 占比统计
	 */
	Private function perceStatis($competeData,$jobKey,$typeMsg){
		
		$perceData = array();
		$perceData['max'] = $perceData['min'] = $perceData['same'] = 0;

		if(!empty($competeData))
		{
			
			
			foreach($competeData as $key=>$value)
			{
				if($key>$jobKey){
					
					$perceData['max'] += $value['perce'];
				
				}elseif($key<$jobKey){
					$perceData['min'] += $value['perce'];
				
				}elseif($key==$jobKey){
					$perceData['same'] = $value['perce'];
				
				}
			
			}
			if(($perceData['same']+$perceData['min'])>$perceData['max']){
				$perceData['type']	=	1;
			}else{
				$perceData['type']	=	2;
			}
		 		}
		
		return $perceData;
	
	}
 
	/**
	 * @desc 数据整理
	 */
	Private function competeStatic($compete,$type,$competeKey,$competeName,$Num){
			
			
			$compete[$type][$competeKey]['name']		= $competeName;
			$compete[$type][$competeKey]['count']++;
			$compete[$type][$competeKey]['perce']		= round($compete[$type][$competeKey]['count']/$Num,4)*100;
			return $compete;
	}
	
}
?>